﻿@*------------------------------------------------------------
  Copyright (c) Microsoft Corporation.  All rights reserved.
  Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
------------------------------------------------------------*@

@page "/supervisors"

@using Microsoft.AspNetCore.Components;
@using Microsoft.Azure.IIoT.App.Services;
@using Microsoft.Azure.IIoT.App.Components.Buttons;
@using Microsoft.Azure.IIoT.App.Components.Drawer;
@using Microsoft.Azure.IIoT.App.Components.ErrorMessage;
@using Microsoft.Azure.IIoT.App.Components.Loader;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry.Models; 
@using Microsoft.Azure.IIoT.App.Common;
@using Microsoft.Azure.IIoT.App.Components.Icons;
@using Microsoft.Azure.IIoT.App.Models;

@inject NavigationManager NavigationManager
@inject Registry RegistryHelper
@inject IRegistryServiceApi RegistryService
@inject RegistryServiceEvents RegistryServiceEvents
@inject UICommon CommonHelper

@inherits IIoTItemsCollection<SupervisorApiModel>

<h1>Twin Modules</h1>

<table class="table table-responsive @_tableView">
    <thead>
        <tr>
            <th class="width-large">Id</th>
            <th class="width-medium">Site Id</th>
            <th class="width-medium">Connection Status</th>
            <th class="width-medium">Sync Status</th>
            <th class="width-small">Version</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var supervisor in Items.Results)
        {
            <tr>
                <td class="hover-text width-large">
                    <div>
                        <a href='endpoints/@RegistryHelper.PathAll/@RegistryHelper.PathAll/@supervisor.Id'>
                            @supervisor.Id
                        </a>
                    </div>
                </td>
                <td class="hover-text width-medium">
                    <div>
                        @(string.IsNullOrEmpty(supervisor.SiteId)
                            ? CommonHelper.None
                            : supervisor.SiteId)
                    </div>
                </td>
                @{string connectStatus = supervisor.Connected == null
                              ? CommonHelper.None
                              : supervisor.Connected == true
                                  ? "Connected"
                                  : "Disconnected";}
                <td class="hover-text width-medium">
                    <ConnectStatus Status="@connectStatus" />
                </td>
                @{string syncStatus = supervisor.OutOfSync == null
                              ? CommonHelper.None
                              : supervisor.OutOfSync == true
                                  ? "Out of Sync"
                                  : "Synced";}
                <td class="hover-text width-medium">
                    <div>
                        @syncStatus
                    </div>
                </td>
                <td class="hover-text width-small">
                    <div>
                        @supervisor.Version
                    </div>
                </td>
                <td>
                    <a class="ellipsis-space" href="javascript: void(0)" @onclick="@(() => OpenDrawer(supervisor.Id))">More Data</a>
                </td>
                <td>
                    <button @onclick="@(() => ResetSupervisorUIAsync(supervisor.Id))" class="btn btn-primary">Reset</button>
                </td>
            </tr>
        }
    </tbody>
</table>
<div class="@_tableEmpty center">No Twin Modules found.</div>

<Drawer HeaderText="Twin Module Status" ObjectData="@SupervisorId" IsOpen="@IsOpen" CloseDrawer="@(() => CloseDrawer())">
    <_DrawerSupervisorContent SupervisorId="@SupervisorId"></_DrawerSupervisorContent>
</Drawer>

<ErrorMessage PageError="@Items.Error" Status="@null"></ErrorMessage>

<LoadMore Result=@Items IsLoading=@IsLoading LoadMoreItems=@(async() => await LoadMoreItems()) />

<Spinner IsLoading=@IsLoading></Spinner>